package com.apptravel.web.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.apptravel.util.Constants;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2012-1-17
 * Time: 10:31:07
 * To change this template use File | Settings | File Templates.
 */
public class AdminInterceptor implements HandlerInterceptor {

    private static final String ADMIN_URL = "/admin";


    /**
	 * 请求之前处理.
	 */
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        String requestUrl = httpServletRequest.getRequestURI();
        if(requestUrl.indexOf(ADMIN_URL) != -1){
            if(requestUrl.indexOf(Constants.LOGIN_URL) > -1){
                return true;
            }
            if(requestUrl.indexOf(Constants.LOGON_URL) > -1){
                return true;
            }
            HttpSession session = httpServletRequest.getSession();
            Object admin = session.getAttribute(Constants.ADMIN_IN_SESSION);
            if(admin == null){
                httpServletResponse.sendRedirect(httpServletRequest.getContextPath()+Constants.LOGIN_URL);
                return false;
            }
            return true;
        }else{
            return true;
        }

    }

    /**
	 * 生成视图前处理.
	 *
	 */
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    /**
	 * 最后执行.
	 *
	 */
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
        //To change body of implemented methods use File | Settings | File Templates.
    }
}
